package com.ruoyi.web.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.domain.SysStudentCourse;
import com.ruoyi.system.service.ISysCourseService;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/course")
public class SysCourseController extends BaseController
{
    @Autowired
    private ISysCourseService courseService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCourse course)
    {
        startPage();
        List<SysCourse> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('system:course:export')")
    @PostMapping("/export")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, SysCourse entity)
    {
        List<SysCourse> list = courseService.selectCourseList(entity);
        ExcelUtil<SysCourse> util = new ExcelUtil<SysCourse>(SysCourse.class);
        util.exportExcel(response, list, "课程管理");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(courseService.selectById(courseId));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCourse course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCourse course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(courseService.deleteCourseByIds(courseIds));
    }

    @Log(title = "课程管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:course:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String courseType) throws Exception
    {
        ExcelUtil<SysCourse> util = new ExcelUtil<SysCourse>(SysCourse.class);
        List<SysCourse> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = courseService.importCourse(dataList, operName, courseType);
        return success(message);
    }

    /**
     * 查询已分配该班级的学员列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/listCourseForCheckbox/{courseType}")
    public TableDataInfo listCourseForCheckbox(@PathVariable("courseType") String courseType)
    {
        List<SysCourse> list = courseService.listCourseForCheckbox(courseType);
        return getDataTable(list);
    }

    /**
     * 查询已分配该班级的学员列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/listCourseOfStudent/{userId}")
    public TableDataInfo listCourseOfStudent(@PathVariable("userId") Long userId)
    {
        List<SysStudentCourse> list = courseService.listCourseOfStudent(userId);
        return getDataTable(list);
    }

    /**
     * 获取册本列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/textbookTree")
    public AjaxResult textbookTree(SysCourse sysCourse)
    {
        return success(courseService.textbookTreeList());
    }
}